package com.wjk.blogs.web.admin;

import com.github.houbb.sensitive.word.bs.SensitiveWordBs;
import com.wjk.blogs.po.Blog;
import com.wjk.blogs.po.User;
import com.wjk.blogs.service.BlogService;
import com.wjk.blogs.service.TagService;
import com.wjk.blogs.service.TypeService;
import com.wjk.blogs.vo.BlogQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.Date;

@Controller
@RequestMapping("/admin")
public class BlogController {

    @Autowired
    private BlogService blogService;

    @Autowired
    private TypeService typeService;

    @Autowired
    private TagService tagService;

    @GetMapping("/blogs")
    public String blogs(Model model,
                        @PageableDefault(
                                size=15,
                                sort = {"updateTime"},
                                direction = Sort.Direction.DESC
                        ) Pageable pageable,
                        BlogQuery blog){
        model.addAttribute("page",blogService.listBlog(pageable,blog));
        model.addAttribute("types",typeService.listType());
        return "admin/blogs";
    }

    @GetMapping("/blogs/input")
    public String input(Model model){

        model.addAttribute("types",typeService.listType());
        model.addAttribute("tags",tagService.listTag());
        model.addAttribute("blog",new Blog());

        return "admin/blogs-input";
    }

    @PostMapping("blogs/search")
    public String searchblogs(Model model,
                        @PageableDefault(
                                size=15,
                                sort = {"updateTime"},
                                direction = Sort.Direction.DESC
                        ) Pageable pageable,
                        BlogQuery blog){
        model.addAttribute("page",blogService.listBlog(pageable,blog));
        return "admin/blogs :: blogList";//返回admin/bogs中的blogList片段
    }

    @PostMapping("blogs")
    public String saveBlog(Blog blog, RedirectAttributes attributes, HttpSession session){
        blog.setUser((User) session.getAttribute("user"));

        blog.setType(typeService.getType(blog.getType().getId()));
        blog.setTags(tagService.getTags(blog.getTagIds()));
        blog.setUpdateTime(new Date());

        Blog blog1;
        if (blog.getId()==null){
            blog.setCreateTime(new Date());

            //敏感词过滤
            String blogC=blog.getContent();
            String blogD=blog.getDescription();
            String resultC= SensitiveWordBs.newInstance().replace(blogC,'0');
            String resultD=SensitiveWordBs.newInstance().replace(blogD,'0');
            blog.setContent(resultC);
            blog.setDescription(resultD);

            blog1=blogService.saveBlog(blog);
            System.out.println("保存成功");
        }
        else{
            blog1=blogService.updateBlog(blog.getId(),blog);
            System.out.println("修改成功");
        }

        if (blog1==null){
            attributes.addFlashAttribute("message","操作失败");
        }else{
            attributes.addFlashAttribute("message","操作成功");
        }

        return "redirect:/admin/blogs";
    }

    @GetMapping("/blogs/{id}/input")
    /*通过 @PathVariable 可以将 URL 中占位符参数绑定到控制器处理方法的入参中:URL 中的 {xxx} 占位符可以通过*/
    public String editInput(@PathVariable Long id, Model model){

        model.addAttribute("types",typeService.listType());
        model.addAttribute("tags",tagService.listTag());


        Blog blog=blogService.getBlog(id);
        blog.init();
        model.addAttribute("blog",blog);
        return "admin/blogs-input";
    }

    @GetMapping("/blogs/{id}/delete")
    public String deleteBlogs(@PathVariable Long id, RedirectAttributes attributes){
        blogService.delete(id);
        attributes.addFlashAttribute("message","删除成功");
        return "redirect:/admin/blogs";
    }



}
